<?php
declare (strict_types = 1);

namespace app\middleware;

use app\server\Token;
use Closure;
use think\Request;
use think\Response;
use think\response\Json;

class CheckToken
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param Closure $next
     *
     * @return mixed|Json
     */
    public function handle(Request $request, Closure $next)
    : mixed {
        //第一步获取token
        $token = $request->header('token');
        //jwt进行校验token
        $res = (new Token())->checkToken($token);
        if ($res['code'] !== 1) {
            return json($res);
        }
        $request->uid = $res['data']->uid;
        return $next($request);
    }
}
